package cn.sundat.web.action;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LogCheckAction extends ActionSupport{

	public String logCheck() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();

		response.setContentType("image/jpeg");
		HttpSession session=request.getSession();
		int width=60;
		int height=20;

		//设置浏览器不要缓存此图片
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		//创建内存图像并获得图形上下文
		BufferedImage image=new BufferedImage(60, 20,BufferedImage.TYPE_INT_RGB);
		Graphics g=image.getGraphics();

		/*
		 * 产生随机验证码
		 * 定义验证码的字符表
		 */
		String chars="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
		char[] rands=new char[4];
		for(int i=0;i<4;i++){
			int rand=(int) (Math.random() *36);
			rands[i]=chars.charAt(rand);
		}

		/*
		 * 产生图像
		 * 画背景
		 */
		g.setColor(new Color(0xDCDCDC));
		g.fillRect(0, 0, 60, 20);

		/*
		 * 随机产生120个干扰点
		 */

		for(int i=0;i<120;i++){
			int x=(int)(Math.random()*60);
			int y=(int)(Math.random()*20);
			int red=(int)(Math.random()*255);
			int green=(int)(Math.random()*255);
			int blue=(int)(Math.random()*255);
			g.setColor(new Color(red,green,blue));
			g.drawOval(x, y, 1, 0);
		}
		g.setColor(Color.BLACK);
		g.setFont(new Font(null, Font.ITALIC|Font.BOLD,18));

		//在不同高度输出验证码的不同字符
		g.drawString(""+rands[0], 1, 17);
		g.drawString(""+rands[1], 16, 15);
		g.drawString(""+rands[2], 31, 18);
		g.drawString(""+rands[3], 46, 16);
		g.dispose();

		//将图像传到客户端
		ServletOutputStream sos=response.getOutputStream();
		ByteArrayOutputStream baos=new ByteArrayOutputStream();
		ImageIO.write(image, "JPEG", baos);
		byte[] buffer=baos.toByteArray();
		response.setContentLength(buffer.length);
		sos.write(buffer);
		baos.close();
		sos.close();

		session.setAttribute("checkcode", new String(rands));

		return NONE;
	}

	/**
	 * 判断验证码是否正确
	 * @return
	 * @throws IOException 
	 */
	public String checkCode() throws IOException {
		//从前台输入的验证码
		String code = ServletActionContext.getRequest().getParameter("code");
		//session里后台放入session的验证码
		String str = (String) ActionContext.getContext().getSession().get("checkcode");
		if(!code.equalsIgnoreCase(str)) {
			//验证码不等于
			ServletActionContext.getResponse().getWriter().print(0);
			return NONE;
		}else {
			//验证码正确
			ServletActionContext.getResponse().getWriter().print(1);
			return NONE;
		}
	}
}
